/* poporcb2.p - PURCHASE ORDER RECEIPT W/ SERIAL NUMBER CONTROL         */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: poporcb2.p 85249 2019-02-27 14:14:12Z r4d                         $: */
/* REVISION: 7.0     LAST MODIFIED: 02/24/92    BY: pma *F085*          */
/* REVISION: 7.0     LAST MODIFIED: 07/29/92    BY: ram *F819*          */
/* REVISION: 7.3     LAST MODIFIED: 08/12/92    BY: tjs *G028*          */
/* REVISION: 7.3     LAST MODIFIED: 09/30/92    BY: afs *G116*          */
/* REVISION: 7.3     LAST MODIFIED: 12/14/92    BY: tjs *G443*          */
/* REVISION: 7.3     LAST MODIFIED: 12/17/92    BY: WUG *G455*          */
/* REVISION: 7.3     LAST MODIFIED: 01/07/93    BY: WUG *G523*          */
/* REVISION: 7.3     LAST MODIFIED: 01/11/93    BY: bcm *G425*          */
/* REVISION: 7.3     LAST MODIFIED: 05/26/93    BY: kgs *GB35*          */
/* REVISION: 7.3     LAST MODIFIED: 09/07/93    BY: WUG *GE83*          */
/* REVISION: 7.4     LAST MODIFIED: 11/23/93    BY: tjs *H082*          */
/* REVISION: 7.4     LAST MODIFIED: 10/23/93    BY: cdt *H184*          */
/* REVISION: 7.4     LAST MODIFIED: 12/20/93    BY: dpm *H074*          */
/* REVISION: 7.4     LAST MODIFIED: 10/11/94    BY: cdt *FS26*          */
/* REVISION: 7.4     LAST MODIFIED: 10/29/94    BY: bcm *FT05*          */
/* REVISION: 7.3     LAST MODIFIED: 11/15/94    BY: bcm *GO37*          */
/* REVISION: 7.4     LAST MODIFIED: 11/18/94    BY: jxz *GO56*          */
/* REVISION: 8.5     LAST MODIFIED: 12/02/94    BY: taf *J038*          */
/* REVISION: 7.4     LAST MODIFIED: 12/21/94    BY: afs *F0BJ*          */
/* REVISION: 7.4     LAST MODIFIED: 03/16/95    BY: smp *F0N5*          */
/* REVISION: 7.4     LAST MODIFIED: 04/18/95    BY: dxk *G0KY*          */
/* REVISION: 7.4     LAST MODIFIED: 08/07/95    BY: jym *G0TP*          */
/* REVISION: 8.5     LAST MODIFIED: 08/22/95    BY: tjs *J06Z*          */
/* REVISION: 8.5     LAST MODIFIED: 10/24/95    BY: taf *J053*          */
/* REVISION: 8.5     LAST MODIFIED: 02/05/96    BY: *J0CV* Robert Wachowicz*/
/* REVISION: 8.5     LAST MODIFIED: 09/26/96    BY: *G2G3* Ajit Deodhar */
/* REVISION: 8.5     LAST MODIFIED: 04/28/97    BY: *G2M7* Suresh Nayak */
/* REVISION: 8.5     LAST MODIFIED: 05/13/97    BY: *G2M4* Ajit Deodhar */
/* REVISION: 8.5     LAST MODIFIED: 10/21/97    BY: *H1G5* Seema Varma  */
/* REVISION: 8.6E    LAST MODIFIED: 05/09/98    BY: *L00Y* Jeff Wootton */
/* REVISION: 8.6E    LAST MODIFIED: 06/17/98    BY: *L020* Charles Yen  */
/* REVISION: 8.6E    LAST MODIFIED: 08/19/98    BY: *L062* Steve Nugent */
/* REVISION: 8.6E    LAST MODIFIED: 08/25/98    BY: *J2WJ* Ajit Deodhar */
/* REVISION: 8.6E    LAST MODIFIED: 09/15/98    BY: *J2ZY* Manish K.    */
/* REVISION: 9.0     LAST MODIFIED: 03/15/99    BY: *M0BG* Jeff Wootton */
/* REVISION: 9.0     LAST MODIFIED: 04/16/99    BY: *J2DG* Reetu Kapoor */
/* REVISION: 9.1     LAST MODIFIED: 10/01/99    BY: *N014* PATTI GAULTNEY     */
/* REVISION: 9.1     LAST MODIFIED: 10/25/99    BY: *N002* Bill Gates   */
/* REVISION: 9.1     LAST MODIFIED: 08/13/00    BY: *N0KQ* myb                */
/* REVISION: 9.1     LAST MODIFIED: 01/12/01    BY: *N0VL* Manish K.          */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Old ECO marker removed, but no ECO header exists *F003*              */
/* Revision: 1.19.1.7     BY: Dan Herman       DATE: 04/17/02  ECO: *P043*    */
/* Revision: 1.19.1.8     BY: Patrick Rowan    DATE: 05/01/02  ECO: *P018*    */
/* Revision: 1.19.1.10    BY: Paul Donnelly    DATE: 06/28/03  ECO: *Q00J*    */
/* Revision: 1.19.1.11    BY: Rajiv Ramaiah    DATE: 08/11/03  ECO: *P0ZK*    */
/* Revision: 1.19.1.12    BY: Priya Idnani     DATE: 03/30/05  ECO: *P3F1*    */
/* Revision: 1.19.1.13    BY: Geeta Kotian     DATE: 09/27/05  ECO: *P431*    */
/* Revision: 1.19.1.14    BY: Changlin Zeng    DATE: 04/26/06  ECO: *R045*    */
/* Revision: 1.19.1.19    BY: Anuradha K.      DATE: 04/16/07  ECO: *P5TF*    */
/* Revision: 1.19.1.21    BY: Prashant Menezes DATE: 09/11/08  ECO: *P6R3*    */
/* Revision: 1.19.1.22    BY: Sandeep Rohila   DATE: 08/14/09  ECO: *Q2VW*    */
/* Revision: 1.19.1.23    BY: Yiqing Chen      DATE: 04/12/10  ECO: *R1Z0*    */
/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}
/* NEW SHARED VARIABLES, BUFFERS AND FRAMES */
{us/ap/apconsdf.i}  

/* SHARED VARIABLES, BUFFERS AND FRAMES */
define     shared variable crtint_amt      like trgl_gl_amt.
define     shared variable dr_acct         like trgl_dr_acct extent 7.
define     shared variable dr_sub          like trgl_dr_sub extent 7.
define     shared variable dr_cc           like trgl_dr_cc extent 7.
define     shared variable eff_date        like glt_effdate.

define     shared variable exch_rate       like exr_rate.
define     shared variable exch_rate2      like exr_rate.
define     shared variable exch_ratetype   like exr_ratetype.
define     shared variable exch_exru_seq   like exru_seq.

define     shared variable is-return       like mfc_logical no-undo.
define     shared variable old_status      like pod_status.
define     shared variable po_recno        as recid.
define     shared variable pod_recno       as recid.
define     shared variable price           like tr_price.
define     shared variable project         like pvo_project.
define     shared variable ps_nbr          like prh_ps_nbr.
define     shared variable rcv_type        like poc_rcv_type.
define     shared variable replace         like mfc_logical.
define     shared variable receivernbr     like prh_receiver.
define     shared variable sct_recno       as recid.
define     shared variable site            like pod_site no-undo.
define     shared variable stdcst          like tr_price.
define     shared variable updt_blnkt      like mfc_logical.
define     shared variable updt_blnkt_list as character no-undo.
define     shared variable yes_char        as character format "x(1)".
define     variable use_pod_um_conv        as logical no-undo.
define     shared variable qopen           like pod_qty_rcvd.
define     shared variable receipt_date    like prh_rcp_date no-undo.
define variable git-stat like mfc_logical no-undo.

define     shared  variable   lgdkey      like lgd_mstr.oid_lgd_mstr no-undo.
define     shared  variable   issueld     as logical no-undo.

define     input parameter ship_date      like prh_ship_date no-undo.
define     input parameter i_shipto       like abs_shipto    no-undo.
define     input parameter consign_flag   like mfc_logical   no-undo.
define     input parameter p_ip_is_usage  like mfc_logical   no-undo.
define     input parameter p_shipper_rec  like mfc_logical   no-undo.
define     input parameter p_int_ref_type like lacd_internal_ref_type   no-undo.
define     input parameter p_abs_um       as   character     no-undo.
define     input parameter p_abs_um_conv  as   decimal       no-undo.
define     input parameter p_abs_um_qty   as   decimal       no-undo.

define     shared workfile tax_wkfl
   field tax_nbr           like pod_nbr
   field tax_line          like pod_line
   field tax_env           like pod_tax_env
   field tax_usage         like pod_tax_usage
   field tax_taxc          like pod_taxc
   field tax_in            like pod_tax_in
   field tax_taxable       like pod_taxable
   field tax_price         like prh_pur_cost.

/* LOCAL VARIABLES, BUFFERS AND FRAMES */
define buffer      poddet      for pod_det.
define buffer      pomstr      for po_mstr.
define variable    over_qty    like pod_qty_rcvd no-undo.
define buffer      lacod_det   for lacod_det.
define buffer      nlacoddet   for lacod_det.
define buffer      lacd_det    for lacd_det.
define buffer      prh_hist    for prh_hist.

define variable use-log-acctg     as logical        no-undo.
define variable lagit             like mfc_logical  no-undo.

/* VALIDATE IF LOGISTICS ACCOUNTING IS TURNED ON */
{us/bbi/gprun.i ""lactrl.p"" "(output use-log-acctg)"}

if use-log-acctg then do:
   /* CHECK IF THIS IS LOGISTICS ACCOUNTING PO SHIPPER GIT RECEIPT PROGRAM */
   {us/bbi/gprun.i ""gpckpprg.p"" "(input 'lagitrc', output lagit)"}
   if not lagit then do:
      /* POSHIVMT IS ALSO MOVING TO TRANSIT */
      {us/bbi/gprun.i ""gpckpprg.p"" "(input 'poshivmt', output lagit)"}
   end.
end.

/* DETERMINE IF SHIPPER IS IN TRANSIT */
if use-log-acctg then do:
   {us/gp/gprunmo.i &module = "LA" &program = ""lagit.p""
              &param = """(output git-stat)"""}
end.

for first poc_ctrl no-lock
   where poc_domain = global_domain:
end.

for first vef_ctrl 
   where vef_domain = global_domain
no-lock: 
end. /* FOR FIRST vef_ctrl */

for po_mstr
   where recid(po_mstr) = po_recno no-lock:
end. /* FOR FIRST PO_MSTR */

find pod_det where recid(pod_det) = pod_recno exclusive-lock no-error.

use_pod_um_conv = no.

if p_int_ref_type <> {&TYPE_POShipper}
then do:
   for first pt_mstr
       where pt_mstr.pt_domain = global_domain and  pt_part = pod_part no-lock:
   end. /* FOR FIRST PT_MSTR */
   if available pt_mstr
      and pod_rum = pt_um
   then
      use_pod_um_conv = yes.
   else
      use_pod_um_conv = no.
end.
else
   use_pod_um_conv = yes.

/* CREATE PURCHASE RECEIPT HISTORY RECORD */
   if pod_qty_chg <> 0 and not git-stat then do:

   /* ADDED SECOND EXCHANGE RATE, TYPE, SEQUENCE */
   /* INSERTED dr_sub[1] AS 13TH PARAMETER IN LIST BELOW */
   /* ADDED 21nd INPUT PARAMETER I_SHIPTO                */
   {us/bbi/gprun.i ""poporcb7.p""
      "(input pod_recno,
        input ship_date,
        input po_recno,
        input sct_recno,
        input rcv_type,
        input site,
        input is-return,
        input (if available vef_ctrl 
                  and vef_using_sup_perf
                  and receipt_date <> ?
               then 
                  receipt_date 
               else 
                  eff_date),
        input pod__qad04[1],
        input receivernbr,
        input ps_nbr,
        input dr_acct[1],
        input dr_sub[1],
        input dr_cc[1],
        input project,
        input price,
        input exch_rate,
        input exch_rate2,
        input exch_ratetype,
        input exch_exru_seq,
        input yes_char,
        input i_shipto,
        input-output crtint_amt,
        input use_pod_um_conv,
        input consign_flag,
        input p_shipper_rec,
        input p_int_ref_type,
        input p_abs_um,
        input p_abs_um_conv,
        input p_abs_um_qty,
        input-output stdcst)"}
   assign pod__qad04[1] = "".
   /* CHANGE LACOD LINK TO PRH_HIST AND RESET LACOD_DET AND LACD_DET */
   if p_int_ref_type = {&TYPE_PO}
   then do:
      find first prh_hist no-lock
           where prh_domain   = global_domain
             and prh_nbr      = pod_nbr
             and prh_receiver = receivernbr
             and prh_line     = pod_line
      no-error.

      if available prh_hist
      then do:
         for each lacd_det exclusive-lock
            where lacd_domain            = global_domain
              and lacd_internal_ref      = pod_nbr
              and lacd_shipfrom          = po_vend
              and lacd_internal_ref_type = {&TYPE_PO}
            :
            find first lacod_det exclusive-lock
                 where lacod_det.oid_lacd_det   = lacd_det.oid_lacd_det
                   and lacod_det.oid_order_line = oid_pod_det
            no-error.
            if available lacod_det
            then do:
               /* CREATE NEW LACOD WITH LINK TO PRH_HIST */
               buffer-copy lacod_det except lacod_det.oid_order_line
                                            lacod_det.oid_lacod_det
                           to nlacoddet.
               assign nlacoddet.oid_order_line = oid_prh_hist.

               /* RESET OLD LACOD */
               assign lacod_det.lacod_apportion_rate = 0
                      lacod_det.lacod_lc_amt         = 0
                      lacod_det.lacod_mod_userid     = global_userid
                      lacod_det.lacod_mod_date       = today
                      lacod_det.lacod_ex_rate        = lacd_ex_rate
                      lacod_det.lacod_ex_rate2       = lacd_ex_rate2
                      .
            end. /* if available lacod_det */

            /* RESET LACD_DET */
            assign lacd_lc_amt = 0.

         end. /* for each lacd_det */
      end. /* if available prh_hist */
   end. /* if p_int_ref_type = {&TYPE_PO} */

end.  /* if pod_qty_chg <> 0 */

/* BLANK OUT pod_so_status FOR CHANGES TO PROPAGATE TO LOWER LEVELS */
/* IN A MULTI-LEVEL EMT SETUP */
for first sod_det
   where sod_domain = global_domain
   and   sod_nbr    = po_so_nbr
   and   sod_line   = pod_line
no-lock:
end.
if available sod_det
then do:
     if can-find(first mfc_ctrl
                    where mfc_domain  = global_domain
                    and   mfc_field   = "soc_emt_ship"
                    and   mfc_logical = yes)    
     and pod_so_status = "S"
     then
        pod_so_status = "P".
end. /* IF AVAILABLE SOD_DET */
else do:
   for first soc_ctrl
      where soc_domain = global_domain
   no-lock:
   end. /* FOR FIRST soc_ctrl */
   for first mfc_ctrl
      where mfc_domain  = global_domain
      and   mfc_field   = "soc_emt_ship"
      and   mfc_logical = yes
   no-lock:
      pod_so_status = "".
   end. /* FOR FIRST mfc_ctrl*/
end. /* ELSE DO */
/* WHEN PO IS RAISED IN STOCKING UM AND RECEIVED OR RETURNED       */
/* IN ALTERNATE UM; CONVERT THE QUANTITY RECEIVED OR RETURNED BY   */
/* DIVIDING WITH CONVERSION FACTOR TO AVOID ROUNDING ERRORS. */

/*UPDATE PURCHASE ORDER QUANTITY RECEIVED OR RETURNED */
if is-return then do:

   if use_pod_um_conv then do:
      if p_shipper_rec
      then do:
         assign
            pod_qty_rtnd   = pod_qty_rtnd + pod_qty_chg
            pod_cum_qty[1] = pod_cum_qty[1] - pod_qty_chg.
      end.
      else do:
         assign
            pod_qty_rtnd = pod_qty_rtnd + pod_qty_chg / pod_um_conv
            pod_cum_qty[1] = pod_cum_qty[1] - pod_qty_chg / pod_um_conv.
      end.
   end. /* IF use_pod_um_conv  */
   else do:
      assign
         pod_qty_rtnd = pod_qty_rtnd + pod_qty_chg * pod_rum_conv
         pod_cum_qty[1] = pod_cum_qty[1] - pod_qty_chg * pod_rum_conv.
   end. /* ELSE DO */
end. /* IF is-return */

else do:
   /* FOR LOGISTICS ACCOUNTING GIT RECEIPT DO NOT UPDATE QTY RECEIVED */
   if not lagit then
   if use_pod_um_conv then do:
      if p_shipper_rec
      then do:
         assign
            pod_qty_rcvd   = pod_qty_rcvd   + pod_qty_chg
            pod_cum_qty[1] = pod_cum_qty[1] + pod_qty_chg.
      end.
      else do:
         assign
            pod_qty_rcvd   = pod_qty_rcvd   + pod_qty_chg / pod_um_conv
            pod_cum_qty[1] = pod_cum_qty[1] + pod_qty_chg / pod_um_conv.
      end.
   end. /* IF USE_POD_UM_CONV */
   else do:
      assign
         pod_qty_rcvd   = pod_qty_rcvd   + pod_qty_chg * pod_rum_conv
         pod_cum_qty[1] = pod_cum_qty[1] + pod_qty_chg * pod_rum_conv.
   end.
end. /* IF NOT is-return */

if is-return and replace and not pod_sched 
then do:
   assign
      po_recno = recid(po_mstr)
      pod_recno = recid(pod_det).
   /* ADDED INPUT PARAMETER use_pod_um_conv TO HANDLE ROUNDING ERROR */
   {us/bbi/gprun.i ""porvisc.p"" "(input use_pod_um_conv,
                                   input lgdkey)"}
end.

if pod_sched then do:
   {us/bbi/gprun.i ""rsmrw.p""
      "(input pod_nbr, input pod_line, input no)"
      }
end.

if pod_blanket <> "" and not is-return then do for poddet:

      find poddet  
         where poddet.pod_domain = global_domain 
          and  poddet.pod_nbr = pod_det.pod_blanket
          and  poddet.pod_line = pod_det.pod_blnkt_ln
      exclusive-lock no-error.

      if available poddet then do:

         /* UPDT_BLNKT_LIST IS A COMMA SEPARATED LIST OF ALL POD_LINE     */
         /* NUMBERS WHICH MUST HAVE THE BLANK PO RELEASE QUANTITY UPDATED */
         if can-do(updt_blnkt_list,string(poddet.pod_line)) 
         then
            updt_blnkt = yes.
         else 
            updt_blnkt = no.

         if updt_blnkt then do for pomstr:

               /* FLIP THE SIGN FOR RETURNS */
               if is-return then
                  pod_det.pod_qty_chg = - pod_det.pod_qty_chg.

               find pomstr
                   where pomstr.po_domain = global_domain 
                    and  pomstr.po_nbr = pod_det.pod_blanket
               exclusive-lock no-error.

               /* Increase blanket released qty by qty overreceived */
               /* Qty overreceived is the larger of this receipt  */
               /* and the total amount overreceived on this line. */
               over_qty = pod_det.pod_qty_chg * pod_det.pod_rum_conv.
               if poddet.pod_um_conv <> pod_det.pod_um_conv then
                  over_qty = over_qty / poddet.pod_rum_conv.
               if over_qty > 0 then
                  over_qty = min(over_qty, pod_det.pod_qty_rcvd - pod_det.pod_qty_ord).
               else
                  over_qty = max(over_qty, pod_det.pod_qty_rcvd - pod_det.pod_qty_ord).

               poddet.pod_rel_qty = poddet.pod_rel_qty + over_qty.

               if pomstr.po_recur
               then
                  poddet.pod_qty_chg = min((poddet.pod_qty_ord - poddet.pod_rel_qty),
                                            poddet.pod_qty_chg).
               if pomstr.po_recur = false
                  or (poddet.pod_qty_ord - poddet.pod_rel_qty <= 0 )
               then poddet.pod_qty_chg = 0.

               if (poddet.pod_qty_ord - poddet.pod_rel_qty <= 0)
               and not lagit
               then poddet.pod_status = "C".

               /* Close Blanket PO if all lines released */
               find first poddet  
                  where poddet.pod_domain = global_domain 
                    and poddet.pod_nbr = pod_det.pod_blanket
                    and poddet.pod_status = "" 
               no-lock no-error.
               if not available poddet then do:
                  assign
                     pomstr.po_cls_date = today
                     pomstr.po_stat = "C".
               end.

               /* RESTORE THE SIGN FOR RETURNS */
               if is-return then
                  pod_det.pod_qty_chg = - pod_det.pod_qty_chg.

            end. /* if updt_blnkt */

            if not lagit
            then do:
               find poddet  
                  where poddet.pod_domain = global_domain 
                  and poddet.pod_nbr  = pod_det.pod_blanket
                  and poddet.pod_line = pod_det.pod_blnkt_ln
               exclusive-lock no-error.

               if poddet.pod_um_conv = pod_det.pod_um_conv then
                  poddet.pod_qty_rcvd = poddet.pod_qty_rcvd
                                      + pod_det.pod_qty_chg
                                      * pod_det.pod_rum_conv.
               else
                  poddet.pod_qty_rcvd = poddet.pod_qty_rcvd
                                      + pod_det.pod_qty_chg
                                      * pod_det.pod_rum_conv
                                      * pod_det.pod_um_conv
                                      / poddet.pod_um_conv.
            end. /* IF NOT lagit */
         end.
      end.

      if not lagit and not pod_sched
      then do:
         if (pod_qty_ord - pod_qty_rcvd) / pod_qty_ord <= 0
         or pod_qty_ord  = 0
         then
            pod_status = "c".

         if pod_bo_chg       = 0
            and (pod_qty_ord - pod_qty_rcvd) / pod_qty_ord > 0
            and old_status       <> "c"
            and old_status       <> "x"
            and pod_rma_type     <>  "O"
            and pod_rma_type     <>  "I"
            and ( not is-return )
            and ((consign_flag and
                  p_ip_is_usage  = no) or
                 (consign_flag   = no))
         then
            pod_status = "x".
      end. /* IF NOT pod_sched */

      if is-return and pod_qty_chg <> 0
         and old_status = "x"  
      then
         pod_status = "x" .
      /* UPDATE MRP AND TRANSACTION HISTORY */
      {us/bbi/gprun.i ""poporcb5.p""}

      /* RESET TEMPORARY VARIABLES */
      assign 
         pod_qty_chg = 0
         pod_bo_chg = 0
         pod_ps_chg = 0
         pod_cost_chg = 0.
